--
-- Author: ping.wang
-- Date: 2018-12-07 14:14:38
--
-- RewardGetBoxRewardView
--
local ShakeConfig = require("app.configs.ShakeConfig")
local Armature = app:getClass("Armature")
local BoxRender = import(".renders.BoxRender")
require("app.ui.DelayCacheMgr")
local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BoxOpenScene

local QualityColor = {
    cc.c3b(72, 255, 0),
    cc.c3b(0, 144, 255),
    cc.c3b(150, 0, 255),
    cc.c3b(255, 216, 1)
}

function ClassRef:init()
	 -- 未打开的宝箱
    self._root:setContentSize(display.width, display.height)
    display.setAutoScale(self._root)
    ccui.Helper:doLayout(self._root)

	AM.playSoundById(2392)
	self._ui.tips:setVisible(false)
	self._ui.Button:setVisible(false)

	self._items = self._data
    self._boxs = {0,0,0,0,0}
    for i,v in ipairs(self._boxs) do
        local panel = self._ui["Panel_" .. i]
        panel:setVisible(false)
    end
    -- box是否打开
    self._boxStatus = {0,0,0,0,0 }

    local params = {
        armatureName = "kbx_qp",
        path = "armatures/effect/kbx_qp"
    }

    local armature = Armature.create(params)
    armature:setPosition(display.width/2,display.height/2)
    self:addChild(armature, 102)

    local ani = {
        name = "kbx_qp",
        playTimes = 1,
        completeCallback = function(sender)
            dump("33333")
            sender:removeFromParent()
        end
    }
    armature:playAni(ani)
    armature:onFrameEvent(function(sender, name)
        if name == "hjm" then
            self:_initDisplayBoxView()
        end
    end)

end

function ClassRef:_initDisplayBoxView()
    for i,v in ipairs(self._boxs) do
        local panel = self._ui["Panel_" .. i]
        panel:setVisible(true)
        display.uiAddClick(panel, function()
            self:_openBox(i)
        end)
        self._boxs[i] = panel
        self:_createBoxArmature(i)
    end

    -- 已打开的宝箱
    self._openBoxs = {0,0,0,0,0}
    for i,v in ipairs(self._openBoxs) do
        local render = BoxRender.create(nil, self._ui["Panel_open_" .. i])
        render:setLocalZOrder(2)
        render:setData(self._items[i])
        self._openBoxs[i] = render
    end
    self:_openAllBox()
end


function ClassRef:_openAllBox()
    self._isOpenAll = true

    local quality = 1
    for i,v in ipairs(self._boxStatus) do
        if v == 0 then
            if self._items[i].quality > quality then
                quality = self._items[i].quality
            end
            self:_openBox(i)
        end
    end

    -- print("_openAllBox quality", quality)

    local conf
    if quality == 5 then
        conf = ShakeConfig:getConfig(11)
    elseif quality == 4 then
        conf = ShakeConfig:getConfig(2)
    else
        conf = ShakeConfig:getConfig(1)
    end
    display.nodeShake(self, conf)
end

function ClassRef:_createBoxArmature(index)
    local panel = self._boxs[index]
    local size = panel:getContentSize()
    local quality = self._items[index].quality

    local params = {
        armatureName = "kbx_xhg",
        path = "armatures/effect/kbx_xhg"
    }
    local armature = Armature.create(params)
    armature:setPosition(size.width/2, size.height/2)
    panel:addChild(armature, -1)
    armature:gotoAndPlay("kbx_xhg", math.random())

    if quality > 1 then
        armature:setColor(QualityColor[quality-1])
    end
end

function ClassRef:_openBox(index)
    -- print("_openBox", index)

    self._boxStatus[index] = 1

    -- 检查是否是最后一个，需要隐藏按钮
    local openNum = 0
    for i,v in ipairs(self._boxStatus) do
        openNum = openNum + v
    end
    local isLastOne = openNum == #self._boxStatus
    if isLastOne then
        self._ui.Button:setVisible(false)
    end

    self:_createOpenArmature(index, isLastOne)
end

function ClassRef:_createOpenArmature(index, isLastOne)
    -- print("_createOpenArmature", index, isLastOne)

    isLastOne = isLastOne or false
    local panel = self._ui.Panel
    local posX = self._boxs[index]:getPositionX()
    local posY = self._boxs[index]:getPositionY()
    local quality = self._items[index].quality

    self._boxs[index]:setVisible(false)

    -- 底板特效
    local params = {
        armatureName = "kbx_zkdh",
        path = "armatures/effect/kbx_zkdh"
    }
    local armBg = Armature.create(params)
    armBg:setPosition(posX, posY)
    panel:addChild(armBg, 1)
    local ani = {
        name = "kbx_zkdh",
        playTimes = 1,
        completeCallback = function(sender)
            local action = cc.FadeIn:create(5/24)
            local box = self._openBoxs[index]
            box:setVisible(true)
            box:setOpacity(0)
            box:runAction(action)
        end
    }
    armBg:playAni(ani)

    -- 根据品质替换不同底板
    if quality > 1 then
        local fileName = "kbx_bg" .. quality
        local params = {
            armatureName = "dh/bg1",
            path = "armatures/effect/" .. fileName
        }
        local arm1 = Armature.create(params)
        armBg:setSlotArmature("xbnxdf", arm1)

        local params = {
            armatureName = "dh/bg2",
            path = "armatures/effect/" .. fileName
        }
        local arm2 = Armature.create(params)
        armBg:setSlotArmature("xbzsd", arm2)
    end

    -- 上层特效
    local params = {
        armatureName = "kbx_zktx",
        path = "armatures/effect/kbx_zktx"
    }
    local armFg = Armature.create(params)
    armFg:setPosition(posX, posY)
    panel:addChild(armFg, 3)
    local ani = {
        name = "kbx_zktx",
        playTimes = 1,
        completeCallback = function(sender)
            sender:removeFromParent()
            if isLastOne then
                self:_openBoxOver()
            end
        end
    }
    armFg:playAni(ani)

    if quality > 1 then
        armFg:setColor(QualityColor[quality-1])
    end

    -- 震屏
    if not self._isOpenAll then
        local conf
        if quality == 5 then
            conf = ShakeConfig:getConfig(11)
        elseif quality == 4 then
            conf = ShakeConfig:getConfig(2)
        else
            conf = ShakeConfig:getConfig(1)
        end
        display.nodeShake(self, conf)
    end
end

function ClassRef:_openBoxOver()
    -- print("_openBoxOver")
    self._ui.tips:setVisible(true)
    self._root:getTimeline():gotoFrameAndPlay(0, true)

    display.uiAddClick(self._ui.Panel, function()
        self:closeView()
    end)

    DelayCacheMgr.unLock(DelayCacheMgr.DelayConstant.GetPartner)
    DelayCacheMgr.unLock(DelayCacheMgr.DelayConstant.GetMount)
end

return ClassRef
